import osg

osg.osgInit(0,[])

n = osg.Node.create()
n_c = osg.ptrGetRefCount(n)

sf = osg.SceneFileHandler.the()
f = sf.read("d.obj")
sf.write(f, "d.osb")


root = osg.Node.create()
gc = osg.Group.create()
root.setCore(gc)

root.addChild(f)
sf.write(root, "f_out.osb")


# Check out ref counting
n = osg.Node.create()
g1 = osg.Group.create()
g2 = osg.Group.create()

n.setCore(g1)
count = osg.ptrGetRefCount(g1)
print "Count: ", count
assert 1 == count
n.setCore(g2)
count = osg.ptrGetRefCount(g1)
print "Count: ", count
assert 1 != count

# Try it now the right way
n = osg.RefPtr(osg.Node.create())
g1 = osg.RefPtr(osg.Group.create())
g2 = osg.RefPtr(osg.Group.create())

assert 1 == osg.ptrGetRefCount(n)
assert 1 == osg.ptrGetRefCount(g1)
assert 1 == osg.ptrGetRefCount(g2)

n.setCore(g1)
assert 2 == osg.ptrGetRefCount(g1)
n.setCore(g2)
assert 1 == osg.ptrGetRefCount(g1)
assert 2 == osg.ptrGetRefCount(g2)
